#include "stdafx.h"

namespace CUIC
{


const TImageInfo* GetImageInfo(CPaintManagerUI *pManager,LPCTSTR pStrImage)
{
	CDuiString sImageName = pStrImage;
	CDuiString sImageResType;
	DWORD dwMask = 0;
	//     bool bTiledX = false;
	//     bool bTiledY = false;

	int image_count = 0;

	CDuiString sItem;
	CDuiString sValue;
	LPTSTR pstr = NULL;


	while( *pStrImage != _T('\0') )
	{
		sItem.Empty();
		sValue.Empty();
		while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') )
		{
			pStrImage = ::CharNext(pStrImage);
		}

		while( *pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' ') )
		{
			LPTSTR pstrTemp = ::CharNext(pStrImage);
			while( pStrImage < pstrTemp)
			{
				sItem += *pStrImage++;
			}
		}

		while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) 
		{
			pStrImage = ::CharNext(pStrImage);
		}

		if( *pStrImage++ != _T('=') ) 
		{
			break;
		}

		while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') )
		{
			pStrImage = ::CharNext(pStrImage);
		}

		if( *pStrImage++ != _T('\'') )
		{
			break;
		}

		while( *pStrImage != _T('\0') && *pStrImage != _T('\'') )
		{
			LPTSTR pstrTemp = ::CharNext(pStrImage);
			while( pStrImage < pstrTemp)
			{
				sValue += *pStrImage++;
			}
		}
		if( *pStrImage++ != _T('\'') )
		{
			break;
		}

		if( !sValue.IsEmpty() ) 
		{
			if( sItem == _T("file") || sItem == _T("res") ) 
			{
				sImageName = sValue;
			}
			else if( sItem == _T("restype") )
			{
				sImageResType = sValue;
			}
			else if( sItem == _T("mask") ) 
			{
				if( sValue[0] == _T('#'))
				{
					dwMask = _tcstoul(sValue.GetData() + 1, &pstr, 16);
				}
				else 
				{
					dwMask = _tcstoul(sValue.GetData(), &pstr, 16);
				}
			}
			// 
			//             else if( sItem == _T("xtiled") )
			// 			{
			//                 bTiledX = (_tcscmp(sValue.GetData(), _T("true")) == 0);
			//             }
			//             else if( sItem == _T("ytiled") )
			// 			{
			//                 bTiledY = (_tcscmp(sValue.GetData(), _T("true")) == 0);
			//             }
		}
		if( *pStrImage++ != _T(' ') ) break;
	}


	const TImageInfo *imageInfo = pManager->GetImage(sImageName);
	if (!imageInfo)
	{
		imageInfo = pManager->AddImage(sImageName,sImageResType.IsEmpty()?NULL:(LPCTSTR)sImageResType,dwMask);
	}


	return imageInfo;
}
}